
import sys
import _acquirepy as acquirepy

acquirepy.acquireCommonInitLibraries()
bhost = "127.0.0.1"
bport = 10000
directory = "/tmp"
commkey = ""
unencrypt = ""
reencrypt = ""
jobident = ""
instanceid = ""
timemax = 0
for i in range(len(sys.argv)):
   targ = sys.argv[i]
   if (len(targ) >= 2):
      if ((targ[0] == '-') and (targ[1] != '-')):
         if (targ[1] == 'n'): bhost = sys.argv[i+1]
         if (targ[1] == 'p'): bport = int(sys.argv[i+1])
         if (targ[1] == 'd'): directory = sys.argv[i+1]
         if (targ[1] == 'c'): commkey = sys.argv[i+1]
         if (targ[1] == 'u'): unencrypt = sys.argv[i+1]
         if (targ[1] == 'r'): reencrypt = sys.argv[i+1]
         if (targ[1] == 'j'): jobident = sys.argv[i+1]
         if (targ[1] == 'i'): instanceid = sys.argv[i+1]
         if (targ[1] == 't'): timemax = int(sys.argv[i+1])

if (timemax == 0):
   print "Warning: time max set to zero, resetting to 24 hours"
   timemax = 3600 * 24

acquirepy.acquireComputeInitState(bhost, bport, commkey, instanceid, "wobble", commkey, None)
acquirepy.acquireComputeUpdateUnencryptReencryptKeys(unencrypt, unencrypt, reencrypt, reencrypt, 1)

datastore_workinstance_info = acquirepy.acquireComputeGetDataStoreForWorkInput()
datastoreid = acquirepy.acquirePythonGetDSWIDataStoreID(datastore_workinstance_info)
localinstancenumber = acquirepy.acquirePythonGetDSWILocalInstanceNumber(datastore_workinstance_info)

datastore_info = acquirepy.acquireCommonGetDataStoreInformation(datastoreid)
blockcount = acquirepy.acquirePythonGetDSBlockCount(datastore_info)
blocksize = acquirepy.acquirePythonGetDSBlockSize(datastore_info)
instancecount = acquirepy.acquirePythonGetDSInstanceCount(datastore_info)
isworkpacket = acquirepy.acquirePythonGetDSIsWorkPacket(datastore_info)
isresults = acquirepy.acquirePythonGetDSIsResults(datastore_info)
iscomplete = acquirepy.acquirePythonGetDSIsComplete(datastore_info)
isvalid = acquirepy.acquirePythonGetDSIsValid(datastore_info)

retcode = acquirepy.acquireCommonDownloadFromDataStore(directory, blockcount, datastoreid, None, unencrypt)

# These can be switched for another version of 'run stuff'
acquirepy.acquireComputeStartProcess(directory, localinstancenumber, instancecount)
retcode = acquirepy.acquireComputeWatchForCancelOrComplete(timemax)

results_datastore_info = acquirepy.acquirePythonCreateDataStore(directory, reencrypt, unencrypt, 0, blocksize)
results_datastore_name = acquirepy.acquirePythonGetDSName(results_datastore_info)
results_blockcount = acquirepy.acquirePythonGetDSBlockCount(results_datastore_info)

retcode = acquirepy.acquirePythonUploadToDataStore(results_datastore_name, directory, blocksize, reencrypt)

acquirepy.acquireCommonSetPurposeFlagsOnDataStore(results_datastore_name, 0, 1, None, 0, 0, None)
#acquirepy.acquireCommonRenameActor(results_datastore_name, "results")

acquirepy.acquireCommonDestroyState()
acquirepy.acquireCommonDestroy()
